package com.project.auth.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.project.auth.mapper.SysRoleMapper;
import com.project.auth.model.system.SysRole;
import com.project.auth.model.system.SysUserRole;
import com.project.auth.service.SysRoleService;
import com.project.auth.service.SysUserRoleService;
import com.project.auth.vo.system.AssginRoleVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Service
public class SysRoleServiceImpl extends ServiceImpl<SysRoleMapper, SysRole> implements SysRoleService {

    @Autowired
    private SysUserRoleService sysUserRoleService;

    @Override
    public Map<String, Object> findRoleDataByUserId(Long id) {
        //查询所有角色，返回list集合
        List<SysRole> allRolesList = baseMapper.selectList(null);
        //设置条件：根据用户id查询关系表，查询对应的角色
        LambdaQueryWrapper<SysUserRole> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(SysUserRole::getUserId, id);
        //根据角色id，找到对应角色信息
        List<SysUserRole> existUserRoleList = sysUserRoleService.list(wrapper);
        //遍历获得id对应的所有角色
        /*List<Long> list = new ArrayList<>();
        for (SysUserRole sysUserRole : existUserRoleList) {
            Long roleId = sysUserRole.getRoleId();
            list.add(roleId);
        }*/
        //将查询出的角色信息，封装为list集合
        List<Long> existRoleIdList = existUserRoleList
                .stream().map(item -> item.getRoleId())
                .collect(Collectors.toList());
        //根据角色id到list集合比较
        List<SysRole> assignRoleList = new ArrayList<>();
        for (SysRole sysRole : allRolesList) {
            //比较
            if (existRoleIdList.contains(sysRole.getId())) {
                assignRoleList.add(sysRole);
            }
        }
        //将所有角色信息和查询出来的角色信息封装到map
        Map<String, Object> roleMap = new HashMap<>();
        roleMap.put("assignRoleList", assignRoleList);
        roleMap.put("allRolesList", allRolesList);

        return roleMap;
    }

    @Override
    public void doAssign(AssginRoleVo assginRoleVo) {
        //将用户原角色数据删除
        LambdaQueryWrapper<SysUserRole> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(SysUserRole::getUserId, assginRoleVo.getUserId());
        sysUserRoleService.remove(wrapper);
        //重新分配角色
        List<Long> roleIdList = assginRoleVo.getRoleIdList();
        for (Long roleId : roleIdList) {
            if (StringUtils.isEmpty(roleId)) {
                continue;
            }
            SysUserRole sysUserRole = new SysUserRole();
            sysUserRole.setUserId(assginRoleVo.getUserId());
            sysUserRole.setRoleId(roleId);
            sysUserRoleService.save(sysUserRole);
        }
    }
}
